﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Telecom.Data;
using Telecom.Service;
using Telecom.Service.EntityCustom;
using TelecomGis.Helper.Control;

namespace TelecomGis.Controllers
{
    public class ONTGponController : ContronllerBase
    {
        private readonly dauNoiTbSer _dauNoiTbSer = new dauNoiTbSer();
        private readonly ONT_GPON_Ser _ONT_GPON_Ser = new ONT_GPON_Ser();
        //
        // GET: /ONTGpon/

        public ActionResult ONTGpon()
        {
            return PartialView("_ONTGpon");
        }

        public ActionResult GianDoGpon()
        {
            return PartialView("_GianDoGpon");
        }

        public string Get_Array_daunoiToJson(string TbId)
        {

            var _listEntity = new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", text = "Tất cả mục chọn", type = "all" });

            string[] stringsRemove = RemoveDuplicates(TbId.Split(','));

            long[] intArray = stringsRemove.Select(x => long.Parse(x)).ToArray();

            var _listEntityReturn = _ONT_GPON_Ser.getListTreeFromArrayTB(intArray);

            _listEntity.AddRange(_listEntityReturn);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string getlistDauNoiTbBySiteid(int siteId)
        {
            var _listEntity = _dauNoiTbSer.getlistBySiteid(siteId);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string getListThueBaoCSSGPON(string mavt, string toql, string donviql)
        {
            string[] toqlCode;
            string[] _donviql;

            if (string.IsNullOrEmpty(toql)) toqlCode = UtContextgis._UserPageContext.ToKtByuser;
            else { List<string> _listtoql = new List<string>(); _listtoql.Add(toql); toqlCode = _listtoql.ToArray(); }

            if (string.IsNullOrEmpty(donviql)) _donviql = UtContextgis._UserPageContext.DonViByUser;
            else { List<string> _listdonvlql = new List<string>(); _listdonvlql.Add(donviql); _donviql = _listdonvlql.ToArray(); }

            var _listEntity = _ONT_GPON_Ser.getListThueBaoCSSGPON(mavt, _donviql, toqlCode);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public ActionResult GetThueBaoCssOntById(long Id)
        {
            var _listEntity = _ONT_GPON_Ser.GetThueBaoCssOntById(Id);
            var jsonResult = Json(_listEntity, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        public string GetAllDNTB()
        {
            var _listEntity = _dauNoiTbSer.getlist();

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public ActionResult ListONTByTbId(int TbId)
        {
            var _listEntity = _ONT_GPON_Ser.getlistByTB(TbId);
            return Json(new { Result = "OK", Records = _listEntity, TotalRecordCount = _listEntity.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateONTGPON(int ONTID)
        {
            var entity = _ONT_GPON_Ser.GetById(ONTID);

            return PartialView("_UpdateONTGPON", entity);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult _UpdateONTGPON(ONT_GPON model)
        {
            if (string.IsNullOrEmpty(model.SERIAL))
            {
                return JsonError("Vui lòng nhập SERIAL");
            }

            if (string.IsNullOrEmpty(model.ACCOUNT))
            {
                return JsonError("Vui lòng nhập ACCOUNT");
            }

            _ONT_GPON_Ser.addorUpdate(model);

            return JsonSuccess("");

        }

        public ActionResult ListONTHUEBAOByTbArrayId(string TbId)
        {

            long[] intArray = TbId.Split(',').Select(x => long.Parse(x)).ToArray();
            var _listEntity = _ONT_GPON_Ser.listONTGPONBYArrayTB(intArray);
            return Json(new { Result = "OK", Records = _listEntity, TotalRecordCount = _listEntity.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ListTHUEBAOONTByTbId(int ontId)
        {
            var _listEntity = _ONT_GPON_Ser.listTHUEBAOGPONBYONT(ontId);
            return Json(new { Result = "OK", Records = _listEntity, TotalRecordCount = _listEntity.Count() }, JsonRequestBehavior.AllowGet);
        }

        public string listJsonIntoCateTree(string toql, string donviql)
        {
            string[] toqlCode;
            string[] _donviql;

            if (string.IsNullOrEmpty(toql)) toqlCode = UtContextgis._UserPageContext.ToKtByuser;
            else { List<string> _listtoql = new List<string>(); _listtoql.Add(toql); toqlCode = _listtoql.ToArray(); }

            if (string.IsNullOrEmpty(donviql)) _donviql = UtContextgis._UserPageContext.DonViByUser;
            else { List<string> _listdonvlql = new List<string>(); _listdonvlql.Add(donviql); _donviql = _listdonvlql.ToArray(); }

            var _listEntity = _dauNoiTbSer.getListTreeAllDauNoiTbParent(toqlCode, _donviql, null, 1);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string listJsonIntoCateTree_Home(string toql, string donviql)
        {
            string[] toqlCode;
            string[] _donviql;

            if (string.IsNullOrEmpty(toql)) toqlCode = UtContextgis._UserPageContext.ToKtByuser;
            else { List<string> _listtoql = new List<string>(); _listtoql.Add(toql); toqlCode = _listtoql.ToArray(); }

            if (string.IsNullOrEmpty(donviql)) _donviql = UtContextgis._UserPageContext.DonViByUser;
            else { List<string> _listdonvlql = new List<string>(); _listdonvlql.Add(donviql); _donviql = _listdonvlql.ToArray(); }

            var _listEntity = _dauNoiTbSer.getListTreeAllDauNoiTb_Home_Page(toqlCode, _donviql, "PON");

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public ActionResult AddThuBaoONTGPOn(int ONTID, int thutu, string donvi, string toql)
        {
            THUEBAO_GPON model = new THUEBAO_GPON();

            var ONTGOPN = _ONT_GPON_Ser.GetById(ONTID);
            var dauNOTb = _dauNoiTbSer.getByid(ONTGOPN.SPLITTER16.Value);

            model.ONT_ID = ONTID;
            model.THUEBAO_ID = 0;
            model.SITE_ID = 0;
            model.ID = 0;

            ViewBag.DOnVIADDTBONT = donvi;
            ViewBag.ToqlADDTBONT = toql;
            ViewBag.ONTSITE = dauNOTb.TEN;
            ViewBag.THUTUONT = thutu;

            return PartialView("_AddThuBaoONTGPOn", model);
        }

        public ActionResult UpdateThuBaoONTGPOn(int ONTID, int thutu,int TbId, string donvi, string toql)
        {
            THUEBAO_GPON model = _ONT_GPON_Ser.GetThueBaoGPonById(TbId);

            var ONTGOPN = _ONT_GPON_Ser.GetById(ONTID);
            var dauNOTb = _dauNoiTbSer.getByid(ONTGOPN.SPLITTER16.Value);


            ViewBag.DOnVIADDTBONT = donvi;
            ViewBag.ToqlADDTBONT = toql;
            ViewBag.ONTSITE = dauNOTb.TEN;
            ViewBag.THUTUONT = thutu;

            return PartialView("_UpdateThuBaoONTGPOn", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddTHUEBAOONT(THUEBAO_GPON model)
        {
            if (model.THUEBAO_ID == 0)
            {
                return JsonError("Vui lòng chọn thuê bao");
            }

            if (model.SITE_ID == 0)
            {
                return JsonError("Vui lòng chọn vnpt site");
            }

            _ONT_GPON_Ser.AddUpdate_THUEBAO_GPON(model);

            return JsonSuccess("");

        }

        [HttpPost]
        public JsonResult _DeleteTHUEBAOONT(int ID)
        {
            try
            {
                Thread.Sleep(50);
                _ONT_GPON_Ser.Dele_THUEBAO_GPON(ID);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }


        public ActionResult LoadViewGianDoGpon(string DauNoid)
        {
            modelGianDoGPOn model = new modelGianDoGPOn();
            model.DauNoiTBId = int.Parse(DauNoid);
            return PartialView("_LoadViewGianDoGpon", model);
        }



        public string LoadJsonGianDoOnGPON(int DNid)
        {
            var _listEntity = _ONT_GPON_Ser.viewGianDoByLapTb(DNid);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });


        }


        public string LoadJsonGianDoOnGPON_BySite(int siteId)
        {

            try
            {
                var entityDauNoiBySite = _dauNoiTbSer.getlistBySiteid(siteId).First();

                var _listEntity = _ONT_GPON_Ser.viewGianDoByLapTb(entityDauNoiBySite.THIETBIID);

                return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                                  new JsonSerializerSettings
                                  {
                                      ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                                  });
            }
            catch (Exception ex)
            {
                List<GianDoEntity> _return = new List<GianDoEntity>();

                return JsonConvert.SerializeObject(_return, Formatting.Indented,
                                  new JsonSerializerSettings
                                  {
                                      ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                                  });
            }


        }

        public ActionResult ViewThueBaoGPON(string laptbId, string tbId)
        {
            modelViewThueBaoOnGPon model = new modelViewThueBaoOnGPon();
            model.thietbiId = laptbId.Split(',');

            if (model.thietbiId.Length == 1)
            {
                model.idMainTB = int.Parse(laptbId);
                return PartialView("_ViewThueBaoGPON_ONE", model);
                
            }
            else
            {
                model.stringArrayTbId = laptbId;
                return PartialView("_ViewThueBaoGPON_RL", model);
            }

            
        }

        public ActionResult ListONTByTbIdSiteId(int TbId)
        {
            var _listEntity = _ONT_GPON_Ser.getlistByTB(TbId);
            return Json(new { Result = "OK", Records = _listEntity, TotalRecordCount = _listEntity.Count() }, JsonRequestBehavior.AllowGet);
        }



    }

    public class modelGianDoGPOn
    {
        public int DauNoiTBId { get; set; }
    }

    public class modelViewThueBaoOnGPon
    {
        public string[] thietbiId { get; set; }
        public string stringArrayTbId { get; set; }
        public int idMainTB { get; set; }
    }
}
